package com.csu.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.Query;
import com.csu.mall.common.utils.R;
import com.csu.mall.product.dao.BrandDao;
import com.csu.mall.product.dao.CategoryBrandRelationDao;
import com.csu.mall.product.dao.CategoryDao;
import com.csu.mall.product.entity.BrandEntity;
import com.csu.mall.product.entity.CategoryBrandRelationEntity;
import com.csu.mall.product.entity.CategoryEntity;
import com.csu.mall.product.service.CategoryBrandRelationService;
import com.csu.mall.product.vo.BrandVo;
import com.csu.mall.product.vo.CatelogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;



@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        String brandIdStr = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandIdStr)){
            Long brandId = Long.parseLong(brandIdStr);
            if(brandId > 0){
                wrapper.eq("brand_id", brandId);
            }
        }

        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
               wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveEntity(CategoryBrandRelationEntity categoryBrandRelation) {
        //如果品牌已经关联过分类，不再关联
        int count = categoryBrandRelationDao.selectCount(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", categoryBrandRelation.getBrandId()).eq("catelog_id", categoryBrandRelation.getCatelogId()));
        if(count > 0) return;
        if(StringUtils.isEmpty(categoryBrandRelation.getBrandName())){
            BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        if(StringUtils.isEmpty(categoryBrandRelation.getCatelogName())){
            CategoryEntity category = categoryDao.selectById(categoryBrandRelation.getCatelogId());
            categoryBrandRelation.setCatelogName(category.getName());
        }
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandVo> getBrandList(Long catId) {
        return brandDao.selectBrands(catId);
    }

    @Override
    public List<CatelogVo> getCatelogList(Long brandId) {
        return categoryDao.selectCatelogVo(brandId);
    }

}